package com.lzy.controller;

import com.alibaba.nacos.api.naming.pojo.healthcheck.impl.Http;
import com.lzy.base.BaseInfoProperties;
import com.lzy.grace.result.GraceJSONResult;
import com.lzy.grace.result.ResponseStatusEnum;
import com.lzy.pojo.Comment;
import com.lzy.pojo.FriendCircleLiked;
import com.lzy.pojo.Users;
import com.lzy.pojo.bo.FriendCircleBO;
import com.lzy.pojo.bo.ModifyUserBO;
import com.lzy.pojo.vo.CommentVO;
import com.lzy.pojo.vo.FriendCircleVO;
import com.lzy.pojo.vo.UsersVO;
import com.lzy.service.CommentService;
import com.lzy.service.FriendCircleService;
import com.lzy.service.UsersService;
import com.lzy.utils.PagedGridResult;
import jakarta.annotation.Resource;
import jakarta.servlet.http.HttpServletRequest;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.time.LocalDateTime;
import java.util.List;
import java.util.UUID;


@RestController
@RequestMapping("friendCircle")
public class FriendCircleController extends BaseInfoProperties {

    @Autowired
    private FriendCircleService friendCircleService;

    @Autowired
    private CommentService commentService;

    @PostMapping("publish")
    public GraceJSONResult publish(@RequestBody FriendCircleBO friendCircleBO,
                                   HttpServletRequest request){

        String userId = request.getHeader(HEADER_USER_ID);

        friendCircleBO.setUserId(userId);
        friendCircleBO.setPublishTime(LocalDateTime.now());

        friendCircleService.publish(friendCircleBO);

        return GraceJSONResult.ok();
    }


    @PostMapping("queryList")
    public GraceJSONResult queryList(String userId,
                                     @RequestParam(defaultValue = "1",name = "page")  Integer page,
                                     @RequestParam(defaultValue = "10",name = "pageSize") Integer pageSize){

        if(StringUtils.isBlank(userId)){
            return GraceJSONResult.error();
        }

        PagedGridResult pagedGridResult = friendCircleService.queryList(userId,page,pageSize);
        List<FriendCircleVO> friendCircleVOList = (List<FriendCircleVO>) pagedGridResult.getRows();


        for (FriendCircleVO friendCircleVO : friendCircleVOList) {

              //获取点赞的朋友列表
            List<FriendCircleLiked> friendCircleLikedList =
                    friendCircleService.queryLikedFriends(friendCircleVO.getFriendCircleId());

            //判断当前用户是否点赞过
            boolean res = friendCircleService.doILike(friendCircleVO.getFriendCircleId(),userId);


            // 获取朋友圈的评论列表
            List<CommentVO> commentVOList = commentService.queryAll(friendCircleVO.getFriendCircleId());

            friendCircleVO.setLikedFriends(friendCircleLikedList);
            friendCircleVO.setDoILike(res);
            friendCircleVO.setCommentList(commentVOList);
        }

        return GraceJSONResult.ok(pagedGridResult);
    }

    @PostMapping("like")
    public GraceJSONResult like(String friendCircleId,
                                  HttpServletRequest request) {

        String userId = request.getHeader(HEADER_USER_ID);
        friendCircleService.like(friendCircleId,userId);
        return GraceJSONResult.ok();
    }
    @PostMapping("unlike")
    public GraceJSONResult unlike(String friendCircleId,
                                  HttpServletRequest request) {

        String userId = request.getHeader(HEADER_USER_ID);
        friendCircleService.unlike(friendCircleId,userId);
        return GraceJSONResult.ok();
    }

    @PostMapping("likedFriends")
    public GraceJSONResult likedFriends(String friendCircleId) {
        List<FriendCircleLiked> friendCircleLikedList =
                friendCircleService.queryLikedFriends(friendCircleId);
        return GraceJSONResult.ok(friendCircleLikedList);
    }

    @PostMapping("delete")
    public GraceJSONResult delete(String friendCircleId,
                                  HttpServletRequest request) {
        String userId = request.getHeader(HEADER_USER_ID);
        friendCircleService.delete(friendCircleId,userId);
        return GraceJSONResult.ok();
    }
}